package com.zsc.koail.camerarecorder;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Binder;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceView;
import android.view.WindowManager;

import com.zsc.koail.camerarecorder.receiver.PhoneReceiver;
import com.zsc.koail.camerarecorder.recorder.Recorder;
import com.zsc.koail.camerarecorder.ui.MainActivity;

import java.util.Date;
import java.util.UUID;

/**
 * @author zsc
 * @create 2018/8/6 0006
 * @Describe
 */
public class RecorderService extends Service {
    private WindowManager windowManager;
    private SurfaceView surfaceView;
    private static final String TAG = "RecorderService";
    private static Recorder mRecorder = new Recorder();

    @Override
    public void onCreate() {
        windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        surfaceView = new SurfaceView(this);
        mRecorder.setRecorderType(Recorder.MEDIA_VIDEO);
        mRecorder.setTargetDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES));
        mRecorder.setTargetName(UUID.randomUUID() + ".mp4");
        mRecorder.setSurfaceView(surfaceView);
        mRecorder.setCameraPosition(1);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                1, 1,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT
        );
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
            layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        }
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        windowManager.addView(surfaceView, layoutParams);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        Notification noti = new Notification.Builder(this)
                .setContentTitle("recorder")
                .setContentText("keep service live")
                .setSmallIcon(R.mipmap.icon)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(12346, noti);
        return Service.START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new RecorderBinder();
    }



    public class RecorderBinder extends Binder {
        public RecorderService getService() {
            return RecorderService.this;
        }
    }

    public void startRecord() {
        if (mRecorder != null)
            mRecorder.record();
    }

    public String stopRecordSave() {
        if (mRecorder != null)
            return mRecorder.stopRecordSave();
        return null;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public static class WarningReceiver extends PhoneReceiver {
        @Override
        protected void onIncomingCallReceived(Context ctx, String number, Date start) {
            //来电
        }

        @Override
        protected void onIncomingCallAnswered(Context ctx, String number, Date start) {
            //来电接通

        }

        @Override
        protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
            //来电结束
        }

        @Override
        protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
            Log.d(TAG, "----------onOutgoingCallStarted------------");
            //去电接通的时候调用
            if (mRecorder != null)
                mRecorder.record();
        }

        @Override
        protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
            Log.d(TAG, "----------onOutgoingCallEnded------------");
            //去电挂断时结束
            if (mRecorder != null)
                mRecorder.stopRecordSave();
        }

        @Override
        protected void onMissedCall(Context ctx, String number, Date start) {

        }
    }
}
